<template>
  <div class="login-container">
    <div class="login-card">
      <h2>{{ isRegisterMode ? '注册账号' : '用户登录' }}</h2>
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="formData.username"
            required
            placeholder="请输入用户名"
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="formData.password"
            required
            placeholder="请输入密码"
          />
        </div>
        <div class="form-group" v-if="isRegisterMode">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="formData.confirmPassword"
            required
            placeholder="请再次输入密码"
          />
        </div>
        <div class="form-group" v-if="isRegisterMode">
          <label for="email">邮箱</label>
          <input
            type="email"
            id="email"
            v-model="formData.email"
            required
            placeholder="请输入邮箱"
          />
        </div>
        <button type="submit" class="submit-btn">{{ isRegisterMode ? '注册' : '登录' }}</button>
      </form>
      <p class="toggle-mode">
        {{ isRegisterMode ? '已有账号？' : '没有账号？' }}
        <button @click="toggleMode" class="toggle-btn">{{ isRegisterMode ? '立即登录' : '立即注册' }}</button>
      </p>
      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

const store = useStore();
const router = useRouter();
const isRegisterMode = ref(false);
const errorMessage = ref('');

const formData = ref({
  username: '',
  password: '',
  confirmPassword: '',
  email: ''
});

const toggleMode = () => {
  isRegisterMode.value = !isRegisterMode.value;
  errorMessage.value = '';
};

const handleSubmit = async () => {
  errorMessage.value = '';

  if (isRegisterMode.value) {
    // 注册逻辑
    if (formData.value.password !== formData.value.confirmPassword) {
      errorMessage.value = '两次密码输入不一致';
      return;
    }

    // 简单验证
    if (formData.value.username.length < 3) {
      errorMessage.value = '用户名至少3个字符';
      return;
    }

    if (formData.value.password.length < 6) {
      errorMessage.value = '密码至少6个字符';
      return;
    }

    try {
      await store.dispatch('register', {
        username: formData.value.username,
        password: formData.value.password,
        email: formData.value.email
      });
      router.push('/');
    } catch (err) {
      errorMessage.value = '注册失败，请重试';
    }
  } else {
    // 登录逻辑
    try {
      // 在实际应用中这里应该有API调用验证用户
      // 简化处理：直接使用输入的用户名密码登录
      await store.dispatch('login', {
        username: formData.value.username,
        password: formData.value.password
      });
      router.push('/');
    } catch (err) {
      errorMessage.value = '用户名或密码错误';
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 60px);
  background-color: #f5f7fa;
  padding: 20px;
}

.login-card {
  background-color: white;
  padding: 2.5rem;
  border-radius: 12px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  color: #2196F3;
  text-align: center;
  margin-bottom: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #4CAF50;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

input:focus {
  outline: none;
  border-color: #2196F3;
}

.submit-btn {
  width: 100%;
  padding: 0.8rem;
  background: linear-gradient(135deg, #4CAF50 0%, #2196F3 100%);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.3s;
}

.submit-btn:hover {
  opacity: 0.9;
}

.toggle-mode {
  text-align: center;
  margin-top: 1.5rem;
  color: #666;
}

.toggle-btn {
  background: none;
  border: none;
  color: #2196F3;
  cursor: pointer;
  font-weight: 500;
  padding: 0;
  margin-left: 0.3rem;
}

.error-message {
  color: #f44336;
  text-align: center;
  margin-top: 1rem;
  padding: 0.5rem;
  border-radius: 4px;
  background-color: #ffebee;
}
</style>